﻿using UnityEngine;
using System.Collections;

public class RegistUI : MonoBehaviour
{
    public GameObject gameAgreementPanel;
    public GameObject okButton;
    public UISprite showPass;
    public UISprite closePass;

    public UISprite showSurePass;
    public UISprite closeSurePass;
    public GameObject userNameInput;
    public GameObject passWordInput;
    public GameObject surePassInput;
    
    private bool isShow = false;
    private bool isShowSurePass = false;
    private string playerName = "";
    private string passWord = "";
    private string surePass = "";
    public GameObject serverPanel;
    //public UILabel errInfo;
    public GameObject close;
    public GameObject userInputPanel;
    public GameObject serverList;

    public LoginUI loginUI;
    public void ErrInfoFalse() 
    {
        //errInfo.gameObject.SetActive(false);
    }

    /// <summary>
    /// 初始化密码显示的状态(当前不显示)
    /// </summary>
    public void InitPassType()
    {
        isShow = false;
        IsShowPassWord(isShow);
    }

    /// <summary>
    /// 关闭当前页面，登录页面再次显示出来
    /// </summary>
    public void OnClose()
    {
        this.gameObject.SetActive(false);
        ErrInfoFalse();
        userInputPanel.GetComponent<UserInput>().ErrInfoFalse();
    }
    /// <summary>
    /// 注册并登录（注册完成后，跳到服务器页面）
    /// </summary>
    public void OnOkButton()
    {
        playerName = userNameInput.GetComponent<UIInput>().value;
        passWord = passWordInput.GetComponent<UIInput>().value;
        surePass = surePassInput.GetComponent<UIInput>().value;
        if (playerName == "" || passWord == "" || surePass == "")
        {
            BoxManager.CreatePopupTextBox("请输入用户名或者密码");
        }
        else if (passWord != surePass)
        {
            BoxManager.CreatePopupTextBox("两次输入的密码不一样");
        }
        else
        {
            StaticData.UserName = playerName;
            StaticData.PassWord = passWord;

            loginUI.manager.SwitchState(Login.LoginState.GetServerList);

            this.gameObject.SetActive(false);
            serverPanel.SetActive(true);

        }
    }

    /// <summary>
    /// 显示密码(当前状态不显示，点击之后显示)
    /// TODO
    /// </summary>
    public void ShowPass()
    {
        isShow = true;
        IsShowPassWord(isShow);
    }
    public void ShowSurePass() 
    {
        isShowSurePass = true;
        IsShowSurePass(isShowSurePass);
        
    }
    private void IsShowSurePass(bool show) 
    {
        if (show)
        {
            showSurePass.gameObject.SetActive(false);
            closeSurePass.gameObject.SetActive(true);
            surePassInput.GetComponent<UIInput>().inputType = UIInput.InputType.Standard;
            surePassInput.GetComponent<UIInput>().UpdateLabel();
        }
        else
        {
            showSurePass.gameObject.SetActive(true);
            closeSurePass.gameObject.SetActive(false);
            surePassInput.GetComponent<UIInput>().inputType = UIInput.InputType.Password;
            surePassInput.GetComponent<UIInput>().UpdateLabel();

        }
    }

    /// <summary>
    /// 不显示密码(当前状态显示密码，点击之后不显示)
    /// </summary>
    public void ClosePass()
    {
        isShow = false;
        IsShowPassWord(isShow);
    }

    public void CloseSurePass() 
    {
        isShowSurePass = false;
        IsShowPassWord(isShowSurePass);
    }
    /// <summary>
    /// 根据show的状态，判断是否显示密码
    /// </summary>
    /// <param name="show"></param>
    private void IsShowPassWord(bool show)
    {
        if (show)
        {
            showPass.gameObject.SetActive(false);
            closePass.gameObject.SetActive(true);
            passWordInput.GetComponent<UIInput>().inputType = UIInput.InputType.Standard;
            passWordInput.GetComponent<UIInput>().UpdateLabel();
        }
        else
        {
            showPass.gameObject.SetActive(true);
            closePass.gameObject.SetActive(false);
            passWordInput.GetComponent<UIInput>().inputType = UIInput.InputType.Password;
            passWordInput.GetComponent<UIInput>().UpdateLabel();

        }
    }

    /// <summary>
    /// 游戏服务协议（本面板中点击“游戏服务协议”）
    /// </summary>
    public void OnAgreementClick()
    {
        gameAgreementPanel.SetActive(true);
    }
}
   
